import { Injectable } from '@angular/core';
import { Subject } from "rxjs/Subject";
import { Observable } from "rxjs/Observable";
import { Http } from "@angular/http";
import { HsClientService } from "./../hs-client.service";
import { SheetDataType, StrategyRequestType } from "./vtradeterminal.model";
import { BASEURL } from "shared";

@Injectable()
export class VtradeTerminalService {
  sheetDataSource; // : Observable<SheetDataType>;
  sheetData: SheetDataType;
  buyinSheetData: SheetDataType;
  saleoutSheetData: SheetDataType;
  buyinSheetDataSource = new Subject();
  saleoutSheetDataSource = new Subject();
  constructor(private hsClient: HsClientService, private http: Http) {
    Observable.zip(
      this.buyinSheetDataSource,
      this.saleoutSheetDataSource,
      (buyinObj, saleoutObj) => Object.assign({}, buyinObj, saleoutObj)
    ).subscribe(res => {
      this.sheetDataSource = Observable.of(res).publishLast();
    });
  }

  getSheetData() {
    return this.sheetData;
  }
  setSheetData(data: SheetDataType) {
    this.sheetData = Object.assign({}, this.sheetData, data);
    this.sheetDataSource = Observable.of(this.sheetData).publishLast();
    console.debug('set sheetData:', this.sheetData);
  }

  // 分析师向后端推送策略消息
  postStrategyMsg(strategyData: StrategyRequestType) {
    let url = `${BASEURL}/strategy/messages?code=001002003001`;
    return this.http.post(url, strategyData, { withCredentials: true });
  }
}